********************************************************************************
*PROJECT: Social Learning of Political Elites								   
*PURPOSE: Prep simulated voting data for dyadic regressions
*			Only need the new dyad-level outcome
********************************************************************************

clear all
pause on
local votingsimfolder = "$path_pch/Data/intermediate/Simulated"
local votinganalyfolder = "$path_pch\Data\analysis\Voting"
timer clear
timer on 1



** Auto-fill 
local pbig = $pcue*100
local pcuelab "`pbig'perc"
local drawstart = $drawstart
local drawend = $drawend


// Read in simulated votes
use "$path_pch/Data/intermediate/Simulated/votes_simulated_RI${drawstart}to${drawend}_`pcuelab'", clear

// Create dyads without MP info
preserve
	forv i=$drawstart/$drawend {
		ren vote_inf`i' vote_inf`i'_d
	}
	ren MP_id MP_id_d
	tempfile using
	save `using'
restore

joinby vote_id bill_id session_id using `using'

tempfile simdyad
save `simdyad', replace


*********************************************************************
// Prep Voting Data (dyads; including lead and lag)
// (Outcome variables: (i) fraction of identical votes (yes/no/abstain)
// (ii) average differences (yes = 1, abstain = 0, no = -1)
// (Comment out after saving)
*********************************************************************

// Consensus votes vs. nonconsensus votes

// Bring voting data created from Saia_prep
use "`votinganalyfolder'/voting_analysis", clear

// Calculate modal vote
bys vote_id vote: gen NumVote = _N
bys vote_id: egen ModalVoteCount = max(NumVote)
bys vote_id: gen totalvote = _N

// collapse, draw histogram
keep vote_id ModalVoteCount totalvote
duplicates drop
gen ModalVoteShare = ModalVoteCount / totalvote


foreach p in p50 p25 {
	preserve
		sum ModalVoteShare, d
		keep if ModalVoteShare < r(`p')
		keep vote_id
		dis _N
		tempfile VS_Vote_`p'
		save `VS_Vote_`p''
	restore
}


use `simdyad', clear

// Collapse info to get 3 summary info on vote similarity (fraction samevote 3cat, fraction samevote 4cat, average vote similarity)
forv i=$drawstart/$drawend {
	gen samevote4c`i' = vote_inf`i' == vote_inf`i'_d
	gen votediff4c`i' = abs(3*(vote_inf`i'=="yes")+2*(vote_inf`i'=="absent")+1*(vote_inf`i'=="abstain")-3*(vote_inf`i'_d=="yes")-2*(vote_inf`i'_d=="absent")-1*(vote_inf`i'_d=="abstain"))
	gen voteSim4c`i' = 3 - votediff4c`i'	
}



// Collapse to get measures: contentious votes (VS, using p50 ~= .66), very contentious votes (VVS, using p25 ~= .52)
forv k = 1/2 {
	local p: word `k' of "p25" "p50"
	local disc: word `k' of "VVS" "VS"
	preserve
		merge n:1 vote_id using `VS_Vote_`p'', nogen keep(3) // keep only contentious votes
		drop vote_id vote_inf* // Drop unncessary variables
		collapse (mean) samevote4c* votediff4c* voteSim4c*, by(session_id bill_id MP_id MP_id_d) // each bill gets the same weight, so collapse by bill first.
		drop bill_id
		collapse (mean) samevote4c* votediff4c* voteSim4c*, by(session_id MP_id MP_id_d) // now collapse to create a session-MP-pair dyads
		keep if MP_id < MP_id_d
		foreach var of varlist samevote4c* votediff4c* voteSim4c* {
			ren `var' `var'_`disc'
		}
		tempfile voting_MP_dyad_`p'
		save `voting_MP_dyad_`p'' 
	restore 
}


// Collapse to get measures: all votes
drop vote_id vote_inf*
collapse (mean) samevote4c* votediff4c* voteSim4c*, by(session_id bill_id MP_id MP_id_d) // each bill gets the same weight, so collapse by bill first.
drop bill_id
collapse (mean) samevote4c* votediff4c* voteSim4c*, by(session_id MP_id MP_id_d) // now collapse to create a session-MP-pair dyads
keep if MP_id < MP_id_d

// Label variables
forv i=$drawstart/$drawend {
	la var voteSim4c`i' "4 Cat vote similarity (3 = same vote, 0 = yes-no) (RI`i')"
	la var samevote4c`i' "Indicating same vote on 4 category voting (RI`i')"	
}


// Merge contentious votes only measures
foreach p in "p25" "p50" {
	merge 1:1 session_id MP_id MP_id_d using `voting_MP_dyad_`p'', nogen
}

foreach var of varlist samevote4c`drawstart'-voteSim4c`drawend'_VS {
	ren `var' `var'_sim
}

save "`votingsimfolder'/sim_MP_dyad_RI${drawstart}to${drawend}_`pcuelab'", replace

beep	
timer off 1
timer list
